﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace AppFuscator
{
	[Serializable]
	public class AppfuscatorConfig
	{
		public static string FileName { get { return Path.Combine(Utils.InternalFilesFolder, "config.xml"); } }
        private static XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(AppfuscatorConfig));

        public string Login { get; set; }
        public string Password { get; set; }

        #region Singleton
        private static volatile AppfuscatorConfig instance;
        private static object syncRoot = new object();
        public static AppfuscatorConfig Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = AppfuscatorConfig.Load();
                    }
                }

                return instance;
            }
        }
        #endregion


        private static AppfuscatorConfig Load()
		{
            AppfuscatorConfig rt;
			if (File.Exists(FileName))
			{
				try
				{
					using (StreamReader read = new StreamReader(File.OpenRead(FileName)))
					{
                        rt = serializer.Deserialize(read) as AppfuscatorConfig;
					}
				}
				catch
				{
                    rt = new AppfuscatorConfig();
				}
			}
			else
			{
                rt = new AppfuscatorConfig();
			}
			return rt;
		}

		public void Save()
		{
			using (StreamWriter write = File.CreateText(FileName))
			{
				serializer.Serialize(write, this);
			}
		}

        public void RemoveLoginData()
        {
            Login = "";
            Password = "";
            Save();
        }
	}
}
